package com.ldwx.center.controller;

import com.ldwx.center.common.entity.Result;
import com.ldwx.center.common.entity.StatusCode;
import com.ldwx.center.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    private FileService fileService;
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public Result uploadFile(@RequestParam(value = "file",required = true) MultipartFile file){

        Map<String,String> map  = fileService.uploadFile(file);
        Result result = new Result();
        if(map.size()>0){
            result.setCode(StatusCode.OK);
            result.setMessage("上传成功");
            result.setData(map);
        }else {
            result.setCode(StatusCode.ERROR);
            result.setMessage("上传失败");
        }
        return result;
    }

    @RequestMapping("download/{name}")
    public void downLoadFile(@PathVariable(value = "name",required = true) String name, HttpServletResponse response){
        fileService.downLoadFile(name,response);


    }
}
